<?php # Корзина
class basket extends Load_use
{
	function getBlock()
	{
		$res = $pr_list = '';
		
		$basket_id = getBasketId();
		$products = getBasketList( $basket_id );
		$costs = getTotalCosts( $basket_id );
		$product_code_str = '';

		foreach( $products['list'] as $product )
		{
			$img = getImageSrcNew( $product->product_code , $size = 'd' );
			$product_fullname 	= $product->prefix.' '.$product->brand.' <strong>'.$product->name.'</strong>';

			$pr_list .= '
			<li class="shopping-cart__list__item orders__products" product_code="'.$product->product_code.'">
				<div class="shopping-cart__list__item__inner">
					<ul>
						<li>
							<div class="global-photo">										
								<img src="'.$img.'" alt="" />
							</div>
							<div class="shopping-cart__list__description">
								<h2>
									<a href="'.$product->url.'">'.$product_fullname.'</a>
								</h2>
							</div>
						</li>
						<li>
							<dl>
								<dt>Кол-во:</dt>
								<dd>
									<ul class="number-navi">
										<li class="number-navi__less arrow">
											<span class="arrow back">-</span>
										</li>
										<li>
											<input type="text" class="select_count product" value="'.$product->count.'" />
										</li>
										<li class="number-navi__more arrow">
											<span class="arrow forward">+</span>
										</li>
									</ul>
								</dd>
							</dl>									
						</li>
						<li>
							<dl class="shopping-cart__list__price">
								<dt>Цена:</dt>
								<dd>'.getPriceFormat( $product->price_retail ).' руб</dd>
							</dl>
						</li>
					</ul>
					<span class="shopping-cart__list__delete delete">
						<span>Удалить</span>
					</span>	
				</div>
			</li>';
			
			$product_code_str .= $product->product_code.','; 
		}

		$res = '<div class="shopping-cart__overlay">&nbsp;</div>
				<div class="shopping-cart__inner">
					<div class="shopping-cart__title">
						<div class="user-panel__item__icon">
							<span>&nbsp;</span>
						</div>
						<h3 class="big-title">Ваша корзина</h3>
						<span class="shopping-cart__close">Закрыть корзину</span>
					</div>	
					<ul class="shopping-cart__list">
						'.$pr_list.'
					</ul>';
					
	$accessories_html = $accessoriesID = '';
	
	$real_product_code_str = substr($product_code_str, 0, -1);	
	
	$this->conn->query( "SELECT prefix, brand, name, accessories FROM `store_products_cache` WHERE `product_code` IN({$real_product_code_str}) and accessories != ''" );
	
	/* echo "SELECT prefix, brand, name, accessories FROM `store_products_cache` WHERE `product_code` IN({$real_product_code_str}) and accessories != ''<br>".$this->conn->rowCount(); */

	while( $row = $this->conn->fetch() ) {
		if($this->conn->rowCount() > 1)
			$accessories_title = 'Не забудьте купить аксессуары';
		else
			$accessories_title = 'Не забудьте купить аксессуары к '.mb_strtolower($row->prefix).' '.$row->brand.' '.$row->name; 
		
		if( $row->accessories )
			$accessoriesID .= $row->accessories.',';
	}
	
	$real_accessoriesID = substr($accessoriesID, 0, -1);	

	$accessories = getProductList( array( 'where' => '`product_code` IN ( '.$real_accessoriesID.' ) and pp in(1,3,4,5)' ) );
			foreach( $accessories['list'] as $product )
			{
				$product_fullname = $product->prefix.' '.$product->brand.' '.$product->name;
				
				if( iconv_strlen($product_fullname, "UTF-8") > 66 )
					$product_fullname = mb_substr($product_fullname, 0, 66).'...';
					
				
				$accessories_html .= '
					<li>
						<input type="hidden" class="product_code" value="'.$product->product_code.'" />
						<div class="global-photo">
							<a target="_blank" href="/'.$product->url.'">
								<img src="'.getImageSrcNew( $product->product_code , $size = 'l' ).'" alt="" />
							</a>
						</div>
						<div class="product-info__goods-list__description">
							<h2>
								<a target="_blank" href="/'.$product->url.'">'.$product_fullname.'</a>
							</h2>
							<strong class="product-info__goods-list__price">'.getPriceFormat( $product->price_retail ).' руб.</strong>
							<button class="global-button green-bg button_add_to_cart">В корзину</button>
						</div>
					</li>';
			}

		if( $accessories_html )
		{
			$res .= ' 
			<div class="basket_accessories">
				<div class="goods-tabs">
					<dl class="goods-tabs__inner">
						<dt class="selected"><h3 class="big-title">'.$accessories_title.'</h3></dt>
						<dd class="selected">
							<div class="sliderkit goods-tabs__slider four-goods">
								<div id="basket_accessories_slide">
									<div class="sliderkit-nav-clip container">
										<ul class="carousel">
										'.$accessories_html.'
										</ul>
									</div>
									<div class="sliderkit-btn sliderkit-nav-btn sliderkit-nav-prev" id="prev">
										<a href="#">
											<span>Предыдущая</span>
										</a>
									</div>
									<div class="sliderkit-btn sliderkit-nav-btn sliderkit-nav-next" id="next">
										<a href="#">
											<span>Следующая</span>
										</a>
									</div>
								</div>
							</div>
						</dd>
					</dl>
				</div>
			</div>';
		}
		
	$res .= '
					<ul class="shopping-cart__navi">
						<li>
							<!--<div class="shopping-cart__navi__home-delivery">
								<input type="radio" id="home-delivery" />
								<label for="home-delivery">Доставить на дом</label>
							</div>-->
							<dl>
								<dt>Итоговая сумма с учетом доставки:</dt>
								<dd><span id="total_price">'.getPriceFormat( $costs['total'] ).'</span> руб</dd>
							</dl>	
						</li>
						<li>
							<a href="#" class="global-button green-bg go-order">
								<span class="global-button__inner one_click" id="one_click_basket">Купить в один клик</span>
							</a>
						</li>
						<li>
							<a href="/order" class="global-button go-order">
								<span class="global-button__inner">Оформить заказ</span>
							</a>
						</li>
					</ul>	
				</div>';
		
		$res_without_product = '
				<div class="shopping-cart__overlay">&nbsp;</div>
				<div class="shopping-cart__inner">
					<div class="shopping-cart__title">
						<div class="user-panel__item__icon">
							<span>&nbsp;</span>
						</div>
						<h3 class="big-title">Ваша корзина пуста</h3>
						<span class="shopping-cart__close">Закрыть корзину</span>
					</div>
				</div>		
				 ';
		
 
		return ($pr_list ? $res : $res_without_product);
	}
}